package com.martinwj.camera2demo.module;

import android.app.Fragment;
import android.app.FragmentManager;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.preference.SwitchPreference;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.martinwj.camera2demo.R;

public class SettingFragment extends PreferenceFragment {


    private SharedPreferences.OnSharedPreferenceChangeListener mPreferenceListener=new SharedPreferences.OnSharedPreferenceChangeListener() {
        @Override
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
            switch (key) {
                case "setting_timer":
                case "setting_awb":
                    ChangeValue(key);
                    break;
            }

        }
    };

    private void ChangeValue(String key) {
        ListPreference listPreference = (ListPreference) findPreference(key);
        CharSequence entry = listPreference.getEntry();
        listPreference.setSummary(entry);
    }
    private SwitchPreference location;
    private ListPreference timer;
    private SwitchPreference audio;
    private final String TAG = "SettingFragment";
    private ListPreference awb;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.setting_layout, container, false);
        ImageView menuBack = view.findViewById(R.id.iv_menu_back);
        menuBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String tag = SettingFragment.class.getSimpleName();
                Fragment settingFragment = getFragmentManager().findFragmentByTag(tag);
                if (settingFragment == null) {
                    return;
                }
                if (settingFragment.isAdded()) {
                    getFragmentManager().popBackStack(tag, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                }
            }
        });
        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.setting);
        PreferenceManager.setDefaultValues(getActivity(),R.xml.setting,false);
        initPreference();
        setValue("setting_timer");
        setValue("setting_awb");
        PreferenceManager.getDefaultSharedPreferences(getActivity()).registerOnSharedPreferenceChangeListener(mPreferenceListener);
    }



    private void setValue(String key) {
        switch (key) {
            case "setting_timer":{
                String[] timeStr = {"关闭", "2秒", "5秒", "10秒"};
                String[] timeInt = {"0", "2", "5", "10"};
                timer.setEntries(timeStr);
                timer.setEntryValues(timeInt);
                setDefault(timer);
            }
            case "setting_awb":{
                String[] awbStr = {"关闭", "自动", "白炽灯", "荧光灯","暖荧光灯","日光灯","阴天","黄昏","阴影"};
                String[] awbInt = {"0", "1", "2", "3","4", "5", "6", "7","8"};
                awb.setEntries(awbStr);
                awb.setEntryValues(awbInt);
                setDefault(awb);
            }

        }
    }

    private void setDefault(ListPreference listPreference) {
        String value = listPreference.getValue();
        if (value == null) {
            listPreference.setValueIndex(0);
        }
        if (listPreference.getEntry() == null) {
            listPreference.setSummary("关闭");
        } else {
            listPreference.setSummary(listPreference.getEntry());
        }
    }

    private void initPreference() {
        audio = (SwitchPreference) findPreference("setting_audio");
        timer = (ListPreference) findPreference("setting_timer");
        awb = (ListPreference) findPreference("setting_awb");
        location = (SwitchPreference) findPreference("setting_location");
    }



}

